package com.github.sbb.sherry.console.service.engine.config;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.sbb.sherry.console.dto.PageList;
import com.github.sbb.sherry.repository.entity.BaseRule;
import com.github.sbb.sherry.repository.entity.BaseRuleExample;
import com.github.sbb.sherry.repository.mapper.BaseRuleMapper;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BaseRuleConfigService {

  @Autowired
  private BaseRuleMapper baseRuleMapper;

  public PageList<BaseRule> pageList(int currentPage, Integer pageSize) {

    if (pageSize == null) {
      pageSize = 10;
    }

    Page<BaseRule> page = PageHelper.startPage(currentPage, pageSize);
    BaseRuleExample example = new BaseRuleExample();
    List<BaseRule> baseRuleList = baseRuleMapper.selectByExample(example);
    if (page.getPages() < currentPage) {
      baseRuleList = new ArrayList<>();
    }
    return PageList.of(currentPage, pageSize, page.getTotal(), baseRuleList);
  }

  public void add(BaseRule baseRule) {
    baseRule.setId(null);
    baseRuleMapper.insertSelective(baseRule);
  }
}

